import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
export default new Vuex.Store({
	state: {
		token: localStorage.getItem('token')
			? localStorage.getItem('token')
			: '',
		moreUserInfo: JSON.parse(localStorage.getItem('moreUserInfo')),
		imgUrl: '',
		// 学科分类
		subject_classificationList: [],
		// 学院信息
		collegeList: [],
		// 成果形式
		achievementTypeOption: [
			{
				value: 'Theoretical research results',
				label: '理论性研究成果',
			},
			{
				value: 'author',
				label: '应用技术成果',
			},
			{
				value: 'Soft science achievements',
				label: '软科学成果',
			},
		],
		// 获奖级别
		awardCategoryOption: [
			{
				value: 'world-class',
				label: '世界级',
			},
			{
				value: 'national level',
				label: '国家级',
			},
			{
				value: 'provincial level',
				label: '省级',
			},
			{
				value: 'district level',
				label: '区级',
			},
			{
				value: 'school level',
				label: '校级',
			},
		],
		// 获奖等级
		awardLevelOption: [
			{
				value: 'grand prize',
				label: '特等奖',
			},
			{
				value: 'first prize',
				label: '一等奖',
			},
			{
				value: 'second prize',
				label: '二等奖',
			},
			{
				value: 'third prize',
				label: '三等奖',
			},
			{
				value: 'Excellence Award',
				label: '优秀奖',
			},
		],
		publish_scopeOptions: [
			{
				value: 'domestic',
				label: '国内',
			},
			{
				value: 'overseas',
				label: '国外',
			},
			{
				value: 'domestic and overseas',
				label: '国内外',
			},
		],
		// 发表范围数据
		patent_scopeOption: [
			{
				value: 'domestic',
				label: '国内',
			},
			{
				value: 'overseas',
				label: '国外',
			},
			{
				value: 'domestic and overseas',
				label: '国内外',
			},
		], // 著作权类型
		copyright_typeOption: [
			{ label: '合作作品', value: 'Collaborative works' },
			{ label: '汇编作品', value: 'Compilation of works' },
			{ label: '委托作品', value: 'Commissioned works' },
			{ label: '视听作品', value: 'Audiovisual works' },
			{ label: '职务作品', value: 'Service works' },
			{ label: '计算机软件', value: 'Computer software' },
		],
	},
	mutations: {
		setToken(state, { token }) {
			localStorage.setItem('token', token);
			state.token = localStorage.getItem('token');
		},
		delToken(state) {
			(state.token = ''), localStorage.removeItem('token');
			localStorage.removeItem('imgUrl');
		},
		setMoreUserInfo(state, { moreUserInfo }) {
			localStorage.setItem('moreUserInfo', JSON.stringify(moreUserInfo));
			state.moreUserInfo = JSON.parse(
				localStorage.getItem('moreUserInfo')
			);
		},
		handleGetSubject_classificationList(state, value) {
			state.subject_classificationList = value;
		},
		handleGetCollegeList(state, value) {
			state.collegeList = value;
		},
	},
	actions: {
		// 获取学科分类信息
		async getSubject_classificationList(context, value) {
			let { _this } = value;
			// console.log(value._this);
			const { data: res } = await _this.$axios.get(
				'/subject_classification'
			);
			if (res.status !== 200) {
				return _this.$message.error('获取学科信息失败');
			}
			let subject_classificationList = res.data;
			subject_classificationList.forEach((item) => {
				item.children = JSON.parse(item.children);
			});
			context.commit(
				'handleGetSubject_classificationList',
				subject_classificationList
			);
		},
		// 获取学院信息
		async getCollegeList(context, value) {
			// console.log(value);
			let { _this } = value;
			let { data: res } = await _this.$axios.get('/college');
			if (res.status !== 200) {
				return _this.$message.error('获取学院失败');
			}
			let cacheData = res.result;
			cacheData.forEach((item) => {
				changeObjName('value', 'label', item);
				changeObjName('name', 'value', item);
			});

			context.commit('handleGetCollegeList', cacheData);

			function changeObjName(oldVal, newVal, obj) {
				for (const key in obj) {
					if (key === oldVal) {
						obj[newVal] = obj[key];
						delete obj[key];
					}
				}
			}
		},
	},
	modules: {},
});
